/*
 * Copyright (c) 2017 RockScript.io.
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package io.rockscript;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.rockscript.service.test.TestError;
import io.rockscript.service.test.TestResult;
import io.rockscript.service.test.TestResults;
import io.rockscript.api.commands.RunTestsCommand;
import io.rockscript.http.client.ClientRequest;
import io.rockscript.http.client.ClientResponse;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class Test extends ClientCommand {

  protected String tests;

  transient protected static Gson prettyGson = new GsonBuilder()
    .setPrettyPrinting()
    .create();

  @Override
  protected void logCommandUsage() {
    log("rock test : Runs a test suite");
    log();
    logCommandUsage("rock test [test options]");
    log();
    log("Example:");
    log("  rock test -n \"*.rst\"");
    log("Runs all test scripts which have a name with .rst extension ");
  }

  @Override
  protected Options getOptions() {
    Options options = super.getOptions();
    options.addOption(Option.builder("n")
      .desc("Test script name pattern. Default is *.rst. Allowed chars are a-zA-Z_0-9*.-_/")
      .hasArg()
      .build());
    return options;
  }

  @Override
  protected void parse(CommandLine commandLine) {
    super.parse(commandLine);
    this.tests = commandLine.getOptionValue("n", null);
  }

  @Override
  public void execute() {
     ClientRequest request = createHttp()
      .newPost(server + "/command")
      .headerContentTypeApplicationJson()
      .bodyJson(new RunTestsCommand()
        .tests(tests)
      );

    log(request);

    ClientResponse response = request.execute();

    log(response);
    log();

    if (response.getStatus()!=200) {
      log("Running the tests failed: "+response.getBody());
    } else {
      TestResults testResults = response.getBodyAs(TestResults.class);

      if (testResults!=null && !testResults.isEmpty()) {
        int errorCount = 0;
        for (TestResult testResult: testResults) {
          if (testResult.hasError()) {
            errorCount++;
            log("XXX "+testResult.getTestName());
            for (TestError error: testResult.getErrors()) {
              log("XXX    "+error);
            }
          } else {
            log(" ✓  "+testResult.getTestName());
          }
        }
        log();
        if (errorCount==0) {
          log("Yippie!!!  All tests passed :)");
        } else {
          log("Damn! "+errorCount+" tests failed :(");
        }
      } else {
        log("No tests were executed");
      }
    }
  }

  public String getTests() {
    return this.tests;
  }
  public void setTests(String tests) {
    this.tests = tests;
  }
  public Test tests(String tests) {
    this.tests = tests;
    return this;
  }
}
